package com.ultrapower.ctool.view;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.ultrapower.ctool.bean.RiChangVO;
import com.ultrapower.ctool.xn_app.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * Created by ctool on 2017/5/12.
 */

public class RiChangPopWindow extends PopupWindow {
    private View contentView;//页面
    private Context context;//上下文
    private View noView;
    private RiChangVO rcVO = new RiChangVO();//参数
    private DatePicker startDatePicker;
    private DatePicker endDatePicker;
    private EditText startTime;//开始时间
    private EditText endTime;//结束时间

    private TextView filterReset;//重置
    private TextView filterSure;//确定

    public RiChangPopWindow(Activity context, final Handler handler){
        this.context=context;
        initViews(handler);
    }

    /**
     * 初始化视图
     */
    private void initViews(final Handler handler) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//加载布局
        contentView = inflater.inflate(R.layout.pop_richang_details, null);//弹框界面
        noView = contentView.findViewById(R.id.popup_richang_noview);//关闭弹框
        filterReset = (TextView) contentView.findViewById(R.id.filter_reset_rc);//重置
        filterSure = (TextView) contentView.findViewById(R.id.filter_sure_rc);//确定
        startTime = (EditText) contentView.findViewById(R.id.start_time_str);
        endTime = (EditText) contentView.findViewById(R.id.end_time_str);
        startDatePicker = (DatePicker) contentView.findViewById(R.id.start_time_rc);//开始时间控件
        endDatePicker = (DatePicker) contentView.findViewById(R.id.end_time_rc);//结束时间控件
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar now = Calendar.getInstance();
        int year=now.get(Calendar.YEAR);
        int monthOfYear=(now.get(Calendar.MONTH) + 1);
        int dayOfMonth=now.get(Calendar.DAY_OF_MONTH);
        startTime.setText("开始时间:    "+now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH) + 1) +"-"+now.get(Calendar.DAY_OF_MONTH));
        endTime.setText("开始时间:    "+now.get(Calendar.YEAR)+"-"+(now.get(Calendar.MONTH) + 1) +"-"+now.get(Calendar.DAY_OF_MONTH));
        rcVO.setStartTime(year + "-" + (monthOfYear<10?"0"+monthOfYear:monthOfYear) + "-" + (dayOfMonth<10?"0"+dayOfMonth:dayOfMonth));
        rcVO.setEndTime(year + "-" + (monthOfYear<10?"0"+monthOfYear:monthOfYear) + "-" + (dayOfMonth<10?"0"+dayOfMonth:dayOfMonth));

        startDatePicker.init(now.get(Calendar.YEAR), now.get(Calendar.MONTH) ,  now.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year,
                int monthOfYear, int dayOfMonth) {
                startTime.setText("开始时间:    "+year+"-"+(monthOfYear+1) +"-"+dayOfMonth);
                rcVO.setStartTime(year + "-" + ((monthOfYear+1)<10?"0"+(monthOfYear+1):(monthOfYear+1)) + "-" + (dayOfMonth<10?"0"+dayOfMonth:dayOfMonth));
                }
        });
        endDatePicker.init(now.get(Calendar.YEAR), now.get(Calendar.MONTH) ,  now.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {
                endTime.setText("结束时间:    "+year+"-"+(monthOfYear+1) +"-"+dayOfMonth);
                rcVO.setEndTime(year + "-" + ((monthOfYear+1)<10?"0"+(monthOfYear+1):(monthOfYear+1)) + "-" + (dayOfMonth<10?"0"+dayOfMonth:dayOfMonth));
            }
        });

        endDatePicker = (DatePicker) contentView.findViewById(R.id.end_time_rc);//结束时间控件
        noView.setOnClickListener(new RiChangPopWindow.CancelOnClickListener());
        contentView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
                    dismiss();
                }
                return true;
            }
        });


        // 重置的点击监听，将所有选项全设为false
        filterReset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startTime.setText("开始时间:");
                endTime.setText("结束时间:");
                rcVO.setEndTime("");
                rcVO.setStartTime("");
            }
        });
        // 确定的点击监听，将所有已选中项列出
        filterSure.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Message msg = new Message();
                Bundle data = new Bundle();
               // System.out.println(rcVO);
                if(rcVO.getEndTime()==null){
                    rcVO.setEndTime("");
                }
                if (rcVO.getStartTime()==null){
                    rcVO.setStartTime("");
                }
                data.putSerializable("rcVO",rcVO);
                msg.setData(data);
                msg.what=1;
                handler.sendMessage(msg);
                dismiss();
            }
        });

        this.setContentView(contentView);
        this.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
        this.setHeight(ActionBar.LayoutParams.MATCH_PARENT);
        ColorDrawable dw = new ColorDrawable(00000000);
        this.setBackgroundDrawable(dw);
        this.setFocusable(true);
        this.setOutsideTouchable(false);
        this.update();

    }

    public class CancelOnClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    }

    public boolean onKeyDown(Context context, int keyCode, KeyEvent event) {
        this.context = context;
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            dismiss();
        }
        return true;
    }

    public void showFilterPopup(View parent) {
        if (!this.isShowing()) {
            this.showAsDropDown(parent);
        } else {
            this.dismiss();
        }
    }

}
